package com.xbai.goods.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.goods.pojo.Album;
import com.xbai.goods.service.AlbumService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jxbai
 * @desc
 * @date 2021/1/27
 */
@RestController
@RequestMapping("/album")
@CrossOrigin
@RequiredArgsConstructor
public class AlbumController {

    private final AlbumService albumService;

    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) Album album, @PathVariable int page, @PathVariable int size) {
        //调用AlbumService实现分页条件查询Album
        PageInfo<Album> pageInfo = albumService.findPage(album, page, size);
        return new Result<>(pageInfo);
    }

    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用AlbumService实现分页查询Album
        PageInfo<Album> pageInfo = albumService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    @PostMapping(value = "/search")
    public Result<List<Album>> findList(@RequestBody(required = false) Album album) {
        //调用AlbumService实现条件查询Album
        List<Album> list = albumService.findList(album);
        return new Result<>(list);
    }

    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        //调用AlbumService实现根据主键删除
        albumService.delete(id);
        return new Result();
    }

    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Album album, @PathVariable Long id) {
        //设置主键值
        album.setId(id);
        //调用AlbumService实现修改Album
        albumService.update(album);
        return new Result();
    }

    @PostMapping
    public Result add(@RequestBody Album album) {
        //调用AlbumService实现添加Album
        albumService.add(album);
        return new Result();
    }

    @GetMapping("/{id}")
    public Result<Album> findById(@PathVariable Long id) {
        //调用AlbumService实现根据主键查询Album
        Album album = albumService.findById(id);
        return new Result<>(album);
    }

    @GetMapping
    public Result<List<Album>> findAll() {
        //调用AlbumService实现查询所有Album
        List<Album> list = albumService.findAll();
        return new Result<>(list);
    }
}
